<?php
class drecord extends spModel
{	
	var $pk = "recordid"; // 每个留言唯一的标志，可以称为主键
	var $table = "drecord"; // 数据表的名称

	//查询出错的网站
	function c_fault()
	{
		$conditions = "status_code<>200";
		$aaa = $this->findAll($conditions);
		$fault = count($aaa);
		return $fault;
	}	
	function c_normal()
	{
		$conditions = "status_code=200";
		$c = $this->findAll($conditions,'','addtime');
		$normal = count($c);
		return $normal;
	}
	function one_fault($siteid)
	{
		$userid =$_SESSION['userinfo']['userid'];
		$conditions = "status_code<>200 and siteid = '$siteid'";
		$c = $this->findAll($conditions,'','addtime');
		$fault = count($c);
		return $fault;
	}
	function one_normal($siteid)
	{
		$conditions = "status_code=200 and siteid = '$siteid'";
		$c = $this->findAll($conditions,'','addtime');
		$normal = count($c);
		return $normal;
	}
	//获取当月的记录
	function n_moth()
	{
		$d_time = spClass('my_fun');
		$t = date("Y-m-d H:i:s",strtotime("0 month"));		
		$f = $d_time->GetTheMonth($t);
		$b = strtotime($f[0]);
		$a = strtotime($f[1]);
		$conditions = "addtime between '$b' and '$a'";
		$n = $this->findAll($conditions);
		return $n;
	}
	//获取上个月的记录
	function n1_moth()
	{
		$d_time = spClass('my_fun');
		$t = date("Y-m-d H:i:s",strtotime("-1 month"));		
		$f = $d_time->GetTheMonth($t);
		$b = strtotime($f[0]);
		$a = strtotime($f[1]);
		$conditions = "addtime between '$b' and '$a'";
		$n = $this->findAll($conditions);
		return $n;
	}
	//获取上上个月的记录
	function n2_moth()
	{
		$d_time = spClass('my_fun');
		$t = date("Y-m-d H:i:s",strtotime("-2 month"));		
		$f = $d_time->GetTheMonth($t);
		$b = strtotime($f[0]);
		$a = strtotime($f[1]);
		$conditions = "addtime between '$b' and '$a'";
		$n = $this->findAll($conditions);
		return $n;
	}
	//删除检测记录
	function del_record($conditions)
	{
		$this->delete($conditions);
	}
	//获取最后两条记录
	function etr($conditions)
	{
		$sort = "recordid DESC ";
		$limit = "1";
		$n = $this->findAll($conditions,$sort,$fields,$limit);
		return $n;
	}
	
	//告警
	function alarm($as)
	{
		if($as['status']<>200)
		{			

				$time = date("Y-m-d H:i:s");
				$news = '出现故障';
				$senmail = spClass('my_fun');
				$senmail->sendmail($news, $time, $as['domain'], $as['username'], $as['mail']);
				
				$alarm = spClass('alarm');
				$conditions = array
				(
					'siteid' => $as['siteid'],
					'news' => $news,
					'user_id' => $as['userid'],
					'addtime' => strtotime(date("Y-m-d H:i:s")),
				);
				$alarm->create($conditions);
				$site = spClass('site');
				$where =array('siteid'=>$as['siteid'],);
				$tst = $site->findAll($where);				
				$dt = array('alarmf'=>$tst['0']['alarmf']+1);
				$site->update($where,$dt);

		}
		else
		{			
				$alarm = spClass('alarm');
				$a_time = $alarm->s_time();
				$n = strtotime(date("Y-m-d H:i:s"));
				$time = $n - $a_time[0][addtime];
				$news = '故障恢复';	
				$senmail = spClass('my_fun');
				$senmail->sendmail($news, $time, $as['domain'], $as['username'], $as['mail']);
				
				
				$conditions = array
				(
					'siteid' => $as['siteid'],
					'news' => $news,
					'user_id' => $as['userid'],
					'addtime' => strtotime(date("Y-m-d H:i:s")),
				);
				$alarm->create($conditions);
				$site = spClass('site');
				$where =array('siteid'=>$as['siteid'],);
				$tst = $site->findAll($where);				
				$dt = array('alarmt'=>$tst['0']['alarmt']+1);
				$site->update($where,$dt);
	
		}
		
	
	}
    
      
      
}